import { goodsList,goodscount } from "../../request/api";

const state={
    list:[],
    size:2,
    page:1,
    total:0,
}
const mutations={
    changeList(state,arr){
        state.list = arr
    },
    changeTotal(state,num){
        state.total = num
    },
    changPage(state,num){
        state.page = num
    }
}
const actions={
    asyncList(context){
        goodsList({size:context.state.size,page:context.state.page}).then(res=>{
            let list = res.data.list?res.data.list:[]
            if(list.length == 0 && context.state.page>1 ){
                context.commit("changePage",context.state.page-1)
                // 再次请求列表数据
                context.dispatch("asyncList")
            }
            context.commit("changeList",list)
        })
    },
    asyncTotal(context){
        goodscount().then(res=>{
          context.commit("changeTotal",res.data.list[0].total)
        })
      },
      // 修改当前页码数
      asyncPage(context,num){
        // 1、修改当前页码数
        context.commit("changPage",num)
        // 2、根据新的页码数请求新的列表数据
        context.dispatch("asyncList")
      }
}
const getters={
    list(state){
        return state.list
    },
    total(state){
        return state.total
      },
      size(state){
        return state.size
      }
}
export default{
    state,
    mutations,
    actions,
    getters,
    namespaced: true

}
